package gold.digger;

import gold.utils.InputUtil;

import java.util.Arrays;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LCJZOF041 {
    public long startExecuteTime = System.currentTimeMillis();


    class MovingAverage {

        int[] store;
        int idx = 0, numCnt = 0;

        /**
         * Initialize your data structure here.
         */
        public MovingAverage(int size) {
            this.store = new int[size];
        }

        public double next(int val) {
            store[idx] = val;
            numCnt++;
            idx = idx >= store.length - 1 ? 0 : idx + 1;
            if (numCnt < store.length) {
                double sum = 0;
                for (int i = 0; i < numCnt; i++) sum += store[i];
                return sum / numCnt;
            }

            return Arrays.stream(store).average().getAsDouble();
        }
    }

    public void run() {
        final MovingAverage movingAverage = new MovingAverage(3);
        movingAverage.next(1); // 返回 1.0 = 1 / 1
        movingAverage.next(10); // 返回 5.5 = (1 + 10) / 2
        movingAverage.next(3); // 返回 4.66667 = (1 + 10 + 3) / 3
        movingAverage.next(5); // 返回 6.0 = (10 + 3 + 5) / 3
    }

    public static void main(String[] args) throws Exception {
        LCJZOF041 an = new LCJZOF041();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
